package com.jule.miaozhuapc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jule.miaozhuapc.util.Const;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jule.miaozhuapc.domain.WawaUser;
import com.jule.miaozhuapc.service.LoginService;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@RequestMapping(value = Const.WAWA_HTTP_URL_V1 + "/login")
@Controller
public class LoginController {
    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private LoginService loginService;

    /**
     * h5 微信登录
     *
     * @param code
     * @param request
     * @param response
     * @throws Exception
     */
    @SuppressWarnings("static-access")
    @RequestMapping(value = "/h5login", method = RequestMethod.GET)
    public @ResponseBody
    void h5login(@RequestParam(name = "code") String code,
                 HttpServletRequest request, HttpServletResponse response,
                 @RequestParam(name = "state", required = false ) String rstUrl) throws Exception {

        logger.info(code);
        logger.info(rstUrl);

        String url2 = request.getScheme()+"://"+ request.getServerName()+request.getRequestURI()+"?"+request.getQueryString();
        logger.info(url2);
        Map<String, String[]> map = request.getParameterMap();
        String baseUrl = "http://www.likewawa.com/mzweb/#/";

        WawaUser wawaUser = loginService.Wxh5Login(code);
        if (wawaUser != null) {
//            if (!StringUtils.isNotEmpty(rstUrl)){
//                rstUrl = "room-list?token=" + wawaUser.getToken();
//            }
//            else {
//
//            }
            rstUrl = rstUrl+"&token="+wawaUser.getToken();
            response.setDateHeader("Expires", System.currentTimeMillis() + 7);
            // response.setHeader("WAWA_TOKEN", wawaUser.getToken());
            logger.info(" 304 " + baseUrl + rstUrl);
            response.sendRedirect(baseUrl + rstUrl);
            return;
        }
        return;
    }
}

